package pojo.subjectentity;

import Mysqlcon.Conn;
import pojo.subjectentity.subjectTitle;

import java.sql.*;
import java.util.Scanner;

public class subjetctinfo extends Conn{

    subjectTitle subtitle = new subjectTitle();

    //课程菜单
    public boolean menu() throws Exception {
        while(true) {
            System.out.println("******欢迎来到课程模块*******");
            System.out.println("1.创建课程");
            System.out.println("2.课程列表展示");
            System.out.println("3.标签管理");
            System.out.println("4.修改课程信息");
            System.out.println("5.退       出");

            System.out.print("请输入:");
            Scanner sc = new Scanner(System.in);
            int i = sc.nextInt();
            if (i == 1) {
                createClass();
            } else if (i == 2) {
                showClass();
            } else if (i == 3) {
                subtitle.menu();
            } else if (i == 4) {
                updateClass();
            } else {
                return false;
            }
        }
    }
    //创建课程
    public void createClass() throws Exception {

                Scanner sc = new Scanner(System.in);
                System.out.println("******欢迎来到创建课程******");
                System.out.print("请输入课程名称:");
                String className = sc.nextLine();
                System.out.print("请输入标签名称:");
                String subjectName = sc.nextLine();

                String sql1 = "select subjectname from title";
                sta = connection.createStatement();
                res = sta.executeQuery(sql1);


                //flag1 判断subjectName是否存在，如果存在，则赋值为false（必须先存在已有标签名） ，不存在，则为true
                boolean flag1 = true;

                //flag2 判断subname是否存在，如果存在，则赋值为 flase ,不能进行下一步操作（不能出现已经存在的课程名)
                boolean flag2 = true;
                while(res.next()) {
                    String tempsubjectName = res.getString("subjectname");
                    if(tempsubjectName.equals(subjectName)){
                        flag1 = false;
                        break;
                    }
                }
                if(!flag1){
                    String sql2 = "select subname from subject";
                    res = sta.executeQuery(sql2);
                    while(res.next()){
                        String tempsubname = res.getString("subname");
                        if(tempsubname.equals(className)){
                            flag2 = false;
                            break;
                        }
                    }
                    if(flag2){
                        try {
                            String sql = "insert into subject(subname,subtitle,createtime) values(?,?,NOW())";
                            prepareStatement = connection.prepareStatement(sql);
                            prepareStatement.setString(1, className);
                            prepareStatement.setString(2, subjectName);
                            int num = prepareStatement.executeUpdate();
                            if (num > 0) {
                                System.out.println("插入成功!");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    else{
                        System.out.println("存在重复的课程名!");
                    }
                }
                else{
                    System.out.println("标签名不存在!");
                }

    }
    //展示课程
    public void showClass() throws Exception {

        try{
            System.out.println("******欢迎来到课程列表展示******");
            sta = connection.createStatement();

            String sql = "select subid,subname,subtitle,createtime from subject";

            ResultSet rs = sta.executeQuery(sql);

            while(rs.next()){
                System.out.println(rs.getInt("subid")+"——>"+"课程名称:"+rs.getString("subname")+";标签名称:"+rs.getString("subtitle")+";创建时间:"+rs.getString("createtime"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void updateClass() throws Exception {
        try{
            Scanner sc = new Scanner(System.in);
            System.out.println("******欢迎来到课程修改信息******");
            System.out.print("请输入原课程名称:");
            String className = sc.nextLine();
            System.out.print("请输入新的标签名称:");
            String newSubjectName = sc.nextLine();

            String sql = "update subject set subtitle = ? where subname = ?";
            prepareStatement = connection.prepareStatement(sql);

            prepareStatement.setString(1,newSubjectName);
            prepareStatement.setString(2,className);
            int num = prepareStatement.executeUpdate();
            if (num > 0) {
                System.out.println("恭喜，班级信息修改完成！");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
